package com.smartstate.EMS.config;

import com.smartstate.common.web.exception.ControllerExceptionHandler;
import com.smartstate.common.web.exception.ResultFactory;
import com.smartstate.common.web.exception.SSSExceptionMapper;
import com.smartstate.common.web.exception.SSSI18nInternational;
import com.smartstate.common.web.httpResult.ApiResultHandler;

import com.smartstate.EMS.util.SerialUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

/**
 * 通用configure
 */
@Configuration
public class Config {
  @Value("${spring.messages.basename}")
  private String basename;

  @Value("${spring.messages.cache-seconds}")
  private long cacheMillis;

  @Value("${spring.messages.encoding}")
  private String encoding;

  /**
   * redis处理对象
   */
  @Autowired
  StringRedisTemplate stringRedisTemplate;

  /**
   * 作用：自动包装成ResultVO，如果想要自己处理。需要添加注解@ApiResultIgnore
   * @return 全局统一结果处理类
   */
  @Bean
  ApiResultHandler getApiResultHandler() {
    return new ApiResultHandler();
  }

  /**
   * 作用：自动生成序列号,默认格式为YYYYMMDD0000,如202203010001
   * @return 序列号
   */
  @Bean
  SerialUtil getSerialUtil() {
    return new SerialUtil(stringRedisTemplate);
  }

  /**
   * 作用：websocket
   */
  @Bean
  public ServerEndpointExporter serverEndpointExporter() {
    ServerEndpointExporter serverEndpointExporter = new ServerEndpointExporter();
    return serverEndpointExporter;
  }

  @Bean(name="redisTemplate")
  public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
    RedisTemplate<String, String> template = new RedisTemplate<>();
    RedisSerializer<String> redisSerializer = new StringRedisSerializer();
    template.setConnectionFactory(factory);
    //key序列化方式
    template.setKeySerializer(redisSerializer);
    //value序列化
    template.setValueSerializer(redisSerializer);
    //value hashmap序列化
    template.setHashValueSerializer(redisSerializer);
    //key haspmap序列化
    template.setHashKeySerializer(redisSerializer);
    //
    return template;
  }

  /**
   * @return 国际化类
   */
  @Bean
  SSSI18nInternational getSSSI18nInternational() {
    return new SSSI18nInternational(basename, cacheMillis, encoding);
  }
  /**
   * @return 异常信息处理类
   */
  @Bean
  SSSExceptionMapper getSSSExceptionMapper() {
    return new SSSExceptionMapper();
  }

  /**
   * @param sssI18nInternational 国际化bean
   * @param sssExceptionMapper   异常映射类
   * @return 异常转换类
   */
  @Bean
  ResultFactory getResultVOFactory(SSSI18nInternational sssI18nInternational,
                                   SSSExceptionMapper sssExceptionMapper) {
    return new ResultFactory(sssI18nInternational, sssExceptionMapper);
  }

  /**
   * 只要控制器之后的异常都可以进行捕获，会自动根据message查找国际化信息并且返回，如果没有定义异常代码，异常代码会是10000，
   * 如果没有定义国际化消息，会默认返回message，支持国际化模板调用
   * @param resultVOFactory 异常转换类
   * @return 全局统一异常处理bean
   */
  @Bean
  ControllerExceptionHandler getControllerExceptionHandler(ResultFactory resultVOFactory) {
    return new ControllerExceptionHandler(resultVOFactory);
  }
}
